<%@ page pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="isperp" uri="/WEB-INF/tlds/isperp.tld"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

<isperp:incidentView>

	<jsp:body> 
	
	 <jsp:include page="step1view.jsp"></jsp:include> 
			
	 <form:form id="incidentHandleForm" commandName="model"
			cssClass="validate" action="incident/incident/save">
            <form:hidden path="id" />
		
<jsp:include page="step2ForEg.jsp"></jsp:include>
			<div id="handerInfoDiv" class="box no-border">
				<div class="header">
					<h3>诊断和解决信息</h3>
				</div> 
<table class="form-table">
   <c:if test="${not empty model.backOpnion }">
    <tr>
      <td class="td1">退回意见：</td>
      <td class="td2" colspan="3">
        <label style="color:red;">${model.backOpnion }</label>
      </td>
    </tr>
   </c:if>
	<tr>
		<td class="td1">故障定位：</td>
		<td class="td2" colspan="3"><form:hidden path="faultlocationId" />
			<isperp:treeSelector width="500" displayValueCssStyle="width:249px"
								url="incident/faultLocation/getTree"
								id="icdfaultlocationSelector" name="faultlocationName"
								displayValue="${model.faultlocationName }"
								valueControlId="faultlocationId"></isperp:treeSelector>
						</td>
	</tr>
	<tr id="assetTr">
		<td class="td1">关联资产：</td>
		<td class="td2"><input id="ciId" name="ciId" class="hidden"
							maxlength="200" value="${model.ciId}" style="width:250px"> <isperp:listSelector
								autocomplete="true" id="ciIdSelector"
								afterSelectEmpty="clearStCi();" displayColumn="code"
								name="ciCode"
								autocompleteDisplay="'【'+item.data['code']+'】'+item.data['name']"
								autocompleteUrl="configuration/ci/autocompletelist"
								valueColumn="id" valueControlId="ciId"
								displayValue="${model.ciId }" displayValueCssStyle="width:249px"
								displayValueCssClass="" url="configuration/ci/listProjectCi?a=1">
				<isperp:column name="id" title="id" hidden="true"></isperp:column>
				<isperp:column name="name" title="名称" width="90">
				</isperp:column>
				<isperp:column name="code" title="编号" width="80">
				</isperp:column>
				<isperp:column name="assetCode" title="序列号" width="80">
				</isperp:column>
				<isperp:column name="status" title="状态" width="80">
				</isperp:column>

			</isperp:listSelector> <a class="nui-txt-link" style="margin-left:-7px" onclick="viewCi()">查看</a>
			</td>
	</tr>

	<tr>
		<td class="td1">原因分析：</td>
		<td class="td2" colspan="3"><form:textarea path="reasonAnalyze"
								cssClass="required" />
						</td>
	</tr>

	<tr>
		<td class="td1">解决方案：</td>
		<td class="td2" colspan="3"><form:textarea path="solution"
								cssClass="required" />
						</td>
	</tr>
	<tr>
		<td class="td1">处理过程：</td>
		<td class="td2" colspan="3"><form:textarea path="handleProcess" />
		</td>
	</tr>
	<tr>
		<td class="td1">解决方式：</td>
		<td class="td2"><form:select path="solutionWay"
								items="${solutionWays}" itemLabel="name" itemValue="name"
								cssClass="required" />
						</td>
		<td class="td1">解决阶段：</td>
		<td class="td2"><form:select path="solutionStage"
								items="${solutionStages}" itemLabel="name" itemValue="name"
								cssClass="required" />
						</td>
	</tr>

	<tr>
	    <td class="td1">工时：</td>
		<td class="td2" ><form:input path="timeCost"
								 cssClass="required number" />
		</td>
		<td class="td1">解决时间：</td>
		<td class="td2" ><form:input path="solvedDate"
								type="datetime" cssClass="required" />
		</td>
	</tr>
	<tr>
		<td class="td1">附件：</td>
		<td class="td2" colspan="3"><isperp:uploader
								sourceId="${model.id }" id="incidentFiles1"></isperp:uploader>
						</td>
	</tr>


</table>
</div>
	</form:form>
	</jsp:body>
</isperp:incidentView>

<isperp:buttonList>
	<isperp:processButtons processKey="incidentProcess" viewOnly="false"
		beforeSubmit="function(name){return checkTask(name);}"
		isValidateFun="function d(name){return isValidateFun(name);}"
		formId="incidentHandleForm" processId="${model.processId }"
		businessKey="${model.id }"></isperp:processButtons>
	<button
		onclick="isperp.openDialog({contentUrl:'ReportServer?reportlet=incident/cpt_incident_print_pd.cpt&op=write&incident_id=${model.id}'})">打印事件单</button>
</isperp:buttonList>

<script type="text/javascript">
    function isValidateFun(name)
	{
	   if(name=="退回一线")
	   {
	     return true;
	   }else
	   {
	     return false;
	   }
	}
	function clearSptOld() {
		$("#sptSelectOldId").val("");
	}
	function clearSptNew() {
		$("#sptSelectNewId").val("");
	}
	function clearStCi() {
		$("#ciIdSelector").val("");
	}
	function assAssectGridSelected(rowid) {
		var rowD = assAssectGrid.getRowData(rowid);
		$("#ciIdSelector").val(rowD.code);
		$("#ciId").val(rowD.id);
	}
	function formatSelector(rowObject, gridId) {
		return "<a href='javascript:void(0);' onclick=\"" + gridId + "Selected('" + rowObject.id + "','" + rowObject.name + "')\">[选择]</a>";
	}
	function formatCiName(rowObject) {
		return "<a href=\"javascript:isperp.openDialog({contentUrl:'configuration/ci/edit?readonly=true&id=" + rowObject.id + "'});\">" + rowObject.name + "</a>";
	}

	function relateProcess(processKey) {
		try {
			isperp.submitForm({
				isValidate : true,
				isCloseDialog : false
			});
		} catch (e) {
		}
		var fromId = $("#id").val();
		var projectId = '${model.id}';
		var url = "workflow/processRun/entry?fromProcessKey=incidentProcess&fromId=" + fromId + "&processKey=" + processKey + "&projectId=" + projectId + "&pcontrolid=processRelationGrid";
		isperp.openDialog({
			url : url
		});
	}

	var result = false;
	function checkTask(name) {
		if (name == "处理完成" && result == false) {
			var id = "${model.id}";
			var unPassNum = 0;
			var unPassNumSpOut = 0;
			var unPassNumSpBack = 0;
			var passNumSpOut = 0;
			var NumSpBack = 0;
			var errMessage = "";

			if ($("#incidentHandleForm").valid() == false) {
				return false;
			}
			var relationProRows = $("#processRelationGrid").jqGrid("getRowData");
			if (relationProRows.length < 1) {
				return true;
			} else {
				jQuery(relationProRows).each(function() {
					if (this.key_ == "taskProcess") {
						if (this.status != "解决" && this.status != "已关闭" && this.status != undefined && this.status != "")
							unPassNum = unPassNum + 1;
					}
					if (this.key_ == "sparepartOutProcess") {
						if (this.status != "已关闭" && this.status != undefined && this.status != "")
							unPassNumSpOut = unPassNumSpOut + 1;
						else if (this.status == "已关闭") {
							passNumSpOut = passNumSpOut + 1;
						}
					}
					if (this.key_ == "sparepartBackProcess") {
						NumSpBack = NumSpBack + 1;
						if (this.status != "已关闭" && this.status != undefined && this.status != "")
							unPassNumSpBack = unPassNumSpBack + 1;
					}
				});
				if (unPassNum > 0) {
					errMessage += "有" + unPassNum + "件未解决的工作单。<br>。";
				} else if (unPassNumSpOut > 0) {
					errMessage += "有关联的出库流程【未关闭】<br>";

				} else if (unPassNumSpBack > 0) {
					errMessage += "有关联的归还流程【未关闭】<br>";
				} else if (passNumSpOut > 0 && NumSpBack == 0) {
					errMessage += "有关联已关闭的领用流程，但没有关联的【归还流程】<br>";
				} else {
					return true;
				}
				if (errMessage != "") {
					isperp.confirmAlert(errMessage + "是否提交？", {
						yes : function() {
							//提交表单
							result = true;
							$("[data-name='处理完成']").click();

						}
					});
				}
			}

			return result;
		} else if (name == "转派") {
			var supporterId = $("#supporterId").val();
			var oldSupporterId = '${model.supporterId}';
			if (supporterId == oldSupporterId) {
				isperp.alert("请选择其他支持人员！");
				return false;
			} else {
				return true;
			}
		} else {
			return true;
		}

	}

	//退回
	function zhuanpaival() {
		if ($("#supporterSelect").val() == "") {
			isperp.alert("支持人员不能为空");
			$("#supporterSelect").focus();
			isperp.isValidate = false;
			return false;
		} else {
			var rs = confirm("确认要转派给【" + $("#supporterSelect").val() + "】吗");
			isperp.isValidate = rs;
			return rs;
		}
	}

	function dealIncident(type) {
		var id = "${model.id}";

		$.post("incident/incident/deal", {
			id : "${model.id}",
			type : type
		}, function(data) {

			if (data.success) {
				window.location.reload();
			} else {
				isperp.alert(data.data);
			}
		});
	}
	var searchKnowledge = function(keyword) {
		if (keyword) {
			knowledgeGrid.jqGrid.setGridParam({
				url : "knowledge/knowledgeSearch/searchByKeyword?keyword=" + keyword
			});
			knowledgeGrid.search();
		}
	};

	$(function() {
	  var key='${model.title}';
	  searchKnowledge(key);
	});
</script>
